package org.example.com.RabbitMq.Controller;

import org.example.com.RabbitMq.config.TopicConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
public class TopicController {
    @Resource
    private AmqpTemplate amqpTemplate;

    @GetMapping("/topic/send/fruit")
    public String sendFruit(String msg){
        System.out.println("send fruit order msg:"+msg);
        amqpTemplate.convertAndSend(TopicConfig.exName,"order.first."+msg,msg);
        try {
            String response = URLDecoder.decode("1", "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return "ok";
    }

    @GetMapping("/topic/send/other")
    public String sendOther(String msg){
        System.out.println("send other order msg:"+msg);
        amqpTemplate.convertAndSend(TopicConfig.exName,"order.other."+msg,msg);
        return "ok";
    }

    //@RabbitListener(queues = TopicConfig.queueOrder1)
    public void process1(String msg){
        System.out.println("Receiver fruit msg:"+ msg);
    }

    //@RabbitListener(queues = TopicConfig.queueOrder2)
    public void process2(String msg){
        System.out.println("Receiver other msg:"+ msg);
    }

}
